﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Param = ClassLibrary.Parameters.Values;
using ClassLibrary;
using System.Threading;
using System.IO;
using System.Globalization;

namespace ControlLibrary
{
    public partial class Form_Parameters : Form
    {

        bool _catTitleModified;
        bool _catDeleted;

        public Form_Parameters(int tab)
        {
            InitializeComponent();

            _catTitleModified = false;
            _catDeleted = false;

            ParametersValue_To_InputForm();
            UpdateUI();
            if(tab >=0 && tab < tabControl1.TabCount)
                tabControl1.SelectedIndex = tab;
        }

        public void UpdateUI()
        {
            listBoxAddSup1.ToolTip_ButtonAdd = Param.RM.GetString("FP_Categorie_Add");
            listBoxAddSup1.ToolTip_ButtonDel = Param.RM.GetString("FP_Categorie_Sup");
            listBoxAddSup1.ToolTip_ButtonUP = Param.RM.GetString("FP_Categorie_Up");
            listBoxAddSup1.ToolTip_ButtonDown = Param.RM.GetString("FP_Categorie_Down");

            listBoxAddSup1.DefaultItemTitle = Param.RM.GetString("FP_Categorie_Default");
        }

        private void ParametersValue_To_InputForm()
        {
            //textBox_nConvert.Text = Parametres.Values.localPath_Exe_nConvert;
            textBox_ftp.Text = Param.FTPServer;
            textBox_login.Text = Param.Login;
            textBox_mdp.Text = Param.MDP;
            textBox_RelativePath.Text = Param.ServerRootFolder;

            //Categories
            //listBoxAddSup1.setDefaultItemTitle("Catégorie ");
            listBoxAddSup1.clearItem();
            foreach (string cat in Param.Categories)
                listBoxAddSup1.addItem(cat);

            listBoxAddSup1.selectedIndex = 0;

            if (Param.PARAM_LANGUAGE == (new CultureInfo("fr-FR").Name))
                comboBox_Language.SelectedIndex = 1;
            else
                comboBox_Language.SelectedIndex = 0;
        }

        private void InputForm_To_ParametersValue()
        {
            //textBox_nConvert.Text = Parametres.Values.localPath_Exe_nConvert;
            Param.FTPServer = textBox_ftp.Text;
            Param.Login = textBox_login.Text;
            Param.MDP = textBox_mdp.Text;
            Param.ServerRootFolder = textBox_RelativePath.Text;

            Param.Categories = listBoxAddSup1.Items;

            if(comboBox_Language.SelectedIndex == 0)
                Param.PARAM_LANGUAGE = new CultureInfo("en-US").Name;
            else Param.PARAM_LANGUAGE = new CultureInfo("fr-FR").Name;
        }

        private void toolStripButton_SaveClose_Click(object sender, EventArgs e)
        {
            InputForm_To_ParametersValue();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void toolStripButton_Cancel_Click(object sender, EventArgs e)
        {
            ParametersValue_To_InputForm();
        }

        private void toolStripButton_Close_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.No;
            this.Close();
        }

        private void ToolStripButton_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void ToolStripButton_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void listBoxAddSup1_itemDeleted(object sender, int iIdx, string title)
        {
            foreach (Marker m in Param.MarkerList)
            {
                int idx = m.categories.IndexOf(title);
                if(idx >= 0)
                    m.categories.RemoveAt(idx);
            }
            _catDeleted = true;
        }

        private void listBoxAddSup1_itemTitleModified(object sender, int iIdx, string oNewtitle, string oOldTitle)
        {
            foreach (Marker m in Param.MarkerList)
            {
                for (int i = 0; i < m.categories.Count; i++)
                {
                    if(m.categories[i] == oOldTitle)
                        m.categories[i] = oNewtitle;
                }
            }
            _catTitleModified = true;
        }

        private void writeMsg(int type, string msg)
        {
            //richTextBox_Message.SelectionStart = 0;

            if (type == -1) richTextBox_res.SelectionColor = Color.Red;
            else if (type == 0) richTextBox_res.SelectionColor = Color.Black;
            else if (type == 1) richTextBox_res.SelectionColor = Color.Blue;
            else if (type == 2) richTextBox_res.SelectionColor = Color.Maroon;
            //richTextBox_Message.AppendText("- " + msg+"\r\n");
            richTextBox_res.SelectedText = msg + "\r\n";
            richTextBox_res.ScrollToCaret();
            this.Refresh();
        }

        private void button_Check_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox_res.Clear();
                //I. Temporistation de 1.5 seconde pour montrer à l'utilisateur que les tests sont en cours            
                this.Cursor = Cursors.WaitCursor;
                label_info.Text = "Tests en cours ...";
                this.Enabled = false;
                label_info.Visible = true;
                Refresh();
                Thread.Sleep(1500);
                InputForm_To_ParametersValue();

                //II. Test des paramètres
                Parameters.Validation res;
                Parameters.check(out res);
                bool RC = true;

                this.Cursor = Cursors.Default;
                this.Enabled = true;

                //III. Affichage des résultats
                //FTP
                //-----------------------------------------------------------------
                if (!res.success_FTPServer_connection)
                    writeMsg(-1, "La connexion au serveur a échoué, vérifiez les paramètres.");

                else
                {
                    //***************************************************************
                    //  LOCAL
                    //***************************************************************
                    if (!res.exists_localPath_Dir_Project || !res.exists_localPath_File_DataSet || !res.exists_localPath_File_GoogleMap)
                        writeMsg(-1, "Fichier(s) ou dossier(s) locaux introuvable(s) :");

                    //Folder DEFAULTFILE
                    //-----------------------------------------------------------------
                    if (!res.exists_localPath_Dir_Project)
                        writeMsg(2, "\t- " + Path.GetFileName(Param.localPath_Project));

                    //File DATASET
                    //-----------------------------------------------------------------
                    if (!res.exists_localPath_File_DataSet)
                        writeMsg(2, "\t- " + Path.GetFileName(Param.localPath_DataSet));

                    //File MYGOOGLEMAP
                    //-----------------------------------------------------------------
                    if (!res.exists_localPath_File_GoogleMap)
                        writeMsg(2, "\t- " + Path.GetFileName(Param.localPath_GoogleMap));

                    //***************************************************************
                    //  SERVER
                    //***************************************************************
                    if (!res.exists_serverPath_Dir_Root || !res.exists_serverPath_Dir_Small || !res.exists_serverPath_Dir_Medium /*|| !res.exists_serverPath_File_DataSet || !res.exists_serverPath_File_GoogleMap*/)
                        writeMsg(-1, "Fichier(s) ou dossier(s) introuvable(s) sur le serveur :");

                    //Folder ROOT
                    //-----------------------------------------------------------------
                    if (!res.exists_serverPath_Dir_Root)
                        writeMsg(2, "\t- " + Param.ServerRootFolder);

                    //Folder SMALL
                    //-----------------------------------------------------------------
                    if (!res.exists_serverPath_Dir_Small)
                        writeMsg(2, "\t- " + Param.ServerPath_Small);

                    //Folder MEDIUM
                    //-----------------------------------------------------------------
                    if (!res.exists_serverPath_Dir_Medium)
                        writeMsg(2, "\t- " + Param.ServerPath_Medium);

                    //File DATASET.JS
                    //-----------------------------------------------------------------
                    /*if (!res.exists_serverPath_File_DataSet)
                        writeMsg(2, "\t- " + Parametres.Values.serverPath_File_DataSet);

                    //File GoogleMap.html
                    //-----------------------------------------------------------------
                    if (!res.exists_serverPath_File_GoogleMap)
                        writeMsg(2, "\t- " + Parametres.Values.serverPath_File_GoogleMap);*/

                    //Tout est ok
                    if (res.all_Valid)
                        writeMsg(1, "Les paramètres sont correctement définis");
                }

                label_info.Visible = false;
            }
            catch (Exception ex)
            {
                ErrorManager.WriteLog("Form_Parameters", "button_Check_Click", ex);
            }
        }

    }
}
